package com.slkj.shilixiaoyuanapp.ui.teacher.tool.homework;

import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.example.api.InjectParam;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.base.ActivityInfo;
import com.slkj.shilixiaoyuanapp.base.BaseActivity;
import com.slkj.shilixiaoyuanapp.base.LazyFragmentPagerAdapter;
import com.slkj.shilixiaoyuanapp.entity.HomeWorkDetailEntity;
import com.slkj.shilixiaoyuanapp.net.HttpHeper;
import com.slkj.shilixiaoyuanapp.net.callback.CommonCallBack;
import com.slkj.shilixiaoyuanapp.ui.common.adapter.ChosePicAdapter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.notice.NoticeHistoryListActivity;
import com.slkj.shilixiaoyuanapp.view.CustomStateText;
import com.slkj.shilixiaoyuanapp.view.StateLayout;
import com.slkj.shilixiaoyuanapp.view.dialog.LoadSuccessAndFailDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindArray;
import butterknife.BindColor;
import butterknife.BindView;
import butterknife.OnClick;

import static com.slkj.shilixiaoyuanapp.entity.code.UiCode.CHOSE_PIC_SPAN_COUNT;

/**
 * 作业详情
 */
@ActivityInfo(layout = R.layout.activity_task_details, title = "作业详情")
public class WorkDetailsActivity extends BaseActivity {
    @InjectParam
    int taskId;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.tv_remark)
    TextView tvRemark;
    @BindView(R.id.recycer_pic)
    RecyclerView recycerPic;
    @BindView(R.id.tab_layout)
    TabLayout tablayout;
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    @BindArray(R.array.tool_work_detail)
    String[] titles;
    @BindView(R.id.layout_root)
    StateLayout stateLayout;
    @BindView(R.id.tv_nj)
    TextView tvNj;
    @BindView(R.id.tv_end_time)
    TextView tvEndTime;
    @BindView(R.id.tv_tx)
    CustomStateText tvTx;
    @BindView(R.id.tv_zsd)
    CustomStateText tvZsd;
    @BindView(R.id.tv_send_time)
    TextView tvSendTime;
    @BindView(R.id.tv_grade)
    TextView tv_grade;
    private List<Fragment> fragments = new ArrayList<>();
    ArrayList<String> picPaths = new ArrayList<>();
    ChosePicAdapter adapter;
    WorkDetailUpLoadFragment workDetailUpLoadFragment;
    WorkDetailUnUpLoadFragment workDetailUnUpLoadFragment;
    @BindColor(R.color.color_ccc)int colorCcc;
    @BindColor(R.color.colorAccent)int colorAccent;
    HomeWorkDetailEntity data;
    @Override
    protected void init() {
        WorkDetailsActivity__JumpCenter.bind(this);

        recycerPic.setLayoutManager(new GridLayoutManager(this, CHOSE_PIC_SPAN_COUNT));
        getData();
        stateLayout.setOnReLoadListener(new StateLayout.OnReLoadListener() {
            @Override
            public void reLoad() {
                getData();
            }
        });
    }

    @Override
    protected void onRightTitleClick() {
        HttpHeper.get().toolService().cjtx(taskId)
                .compose(bindToLifecycle())
                .compose(getThread())
                .subscribe(new CommonCallBack<String>(true, this) {
                    @Override
                    public void onCallBackSuccess(String data) {
                        LoadSuccessAndFailDialog.showSuccess(WorkDetailsActivity.this,data);

                    }
                });
    }

    private void getData() {
        HttpHeper.get().toolService()
                .getHomeWorkDetail(taskId)
                .compose(bindToLifecycle())
                .compose(getThread())
                .subscribe(new CommonCallBack<HomeWorkDetailEntity>(true, stateLayout) {
                    @Override
                    public void onCallBackSuccess(HomeWorkDetailEntity back) {
                        data = back;
                        setViewData(data);
                    }
                    @Override
                    public void onFail(String mes) {
                        stateLayout.showErrorView();
                    }
                });
    }

    public void setViewData(HomeWorkDetailEntity viewData) {
        if (!viewData.getImg().isEmpty()) {
            adapter = new ChosePicAdapter(viewData.getImg(), this, false);
            recycerPic.setAdapter(adapter);
        }
        tv_grade.setText(viewData.getClassName());
        tvType.setText(viewData.getHType());
        tvNj.setText(viewData.getSubjectName());
        tvEndTime.setText("截至 "+viewData.getEndTime());
        tvRemark.setText(viewData.getBody());
        tvZsd.setText(viewData.getKnowledge());
        tvSendTime.setText(viewData.getInTime());
        tvTx.setText(viewData.getShows());
        tvTx.setStrokeColor(!viewData.isCanTx()?colorCcc:colorAccent);
        tvTx.setTextColor(!viewData.isCanTx()?colorCcc:colorAccent);
        tvType.setText(viewData.getHType());
        stateLayout.showContentView();
        workDetailUpLoadFragment = new WorkDetailUpLoadFragment();
        fragments.add(workDetailUpLoadFragment);
        workDetailUnUpLoadFragment = new WorkDetailUnUpLoadFragment();
        fragments.add(workDetailUnUpLoadFragment);
        workDetailUpLoadFragment.onDataBack(viewData.getDelivered());
        workDetailUnUpLoadFragment.onDataBack(viewData.getUnpaid());
        viewpager.setAdapter(new LazyFragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return titles[position];
            }
        });
        tablayout.setupWithViewPager(viewpager);

    }
    @OnClick(R.id.tv_tx)void tx(){
        if (data.isCanTx()){
            HttpHeper.get().toolService().cjtx(data.getHId())
                    .compose(bindToLifecycle())
                    .compose(getThread())
                    .subscribe(new CommonCallBack<String>(true, WorkDetailsActivity.this) {
                        @Override
                        public void onCallBackSuccess(String data) {
                            LoadSuccessAndFailDialog.showSuccess(WorkDetailsActivity.this,data);
                            tvTx.setText(data);
                            tvTx.setStrokeColor(colorCcc);
                            tvTx.setTextColor(colorCcc);
                        }
                    });
        }
    }
}
